<?php

    require_once 'Nusoap/nusoap.php';

    /**
     * Nusoap
     *
     * @package      Core
     * @subpackage   Soap_Adapter
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
	class Core_Soap_Adapter_Nusoap extends Core_Soap_Adapter_Abstract
	{

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

	    /**
         * Calls the SOAP server with the requested parameters
         *
         * @param       string      $method
         * @param       array       $params
         * @throws      Core_Exception
         * @throws      Core_ArgumentException
         * @throws      Core_ArgumentNullException
         * @return      array
         */
        public function call($method, array $params = array())
        {
            if ($method === null) {
                throw new Core_ArgumentNullException('method is a null reference');
            }

            if (strlen(trim($method)) == 0) {
                throw new Core_ArgumentException('method is a zero-length string');
            }

            if ($this->_url === null) {
                throw new Core_ArgumentNullException('Url must be implemented in subclasses');
            }

            if (strlen(trim($this->_url)) == 0) {
                throw new Core_ArgumentException('Url must be implemented in subclasses');
            }

            $client = new nusoap_client($this->_url, true);

            // UTF-8
            $client->soap_defencoding   = 'UTF-8';
            $client->decode_utf8        = false;
            
            $error  = $client->getError();

            if ($error) {
                throw new Core_Exception($error);
            } else {
                $client->setUseCurl(false);

                $result = $client->call($method, array($params));

                if ($client->fault) {
                    throw new Core_Exception($client->fault);
                } else {
                    $error = $client->getError();

                    if ($error) {
                        throw new Core_Exception($error);
                    } else {
                        return $result;
                    }
                }
            }

            return array();
        }
	}
